package com.lwd1513.designPattern.factory.factorymethod;

/**
 * @Author: 刘东（挂兰）
 * @Date: 2023/11/16 23:29:21
 * @Description: 工厂方法个人理解
 */

/**
 * 奶茶店
 */
public class MilkTeaStore {

    public static void main(String[] args) {
        new MilkTeaStore().order(new StrawberryFactory());
        new MilkTeaStore().order(new WaterMelonFactory());
        /**
         * 优点 有新增的奶茶不需要修改代码(if) 只要新增新的奶茶类和奶茶类对于的工厂方法就好了
         */
    }

    public void order(AbstractFactory abstractFactory) {
        // 获取不同口味的奶茶奶茶
        MilkTea milkTea = produceMilkTea(abstractFactory);
        // 奶茶加工
        processMilkTea(milkTea);
    }

    /**
     * @param abstractFactory 入参是抽象抽象类 通过传入抽象类的实现类定制工厂方法返回的值
     * @return
     */
    public MilkTea produceMilkTea(AbstractFactory abstractFactory) {
        return abstractFactory.productMilkTea();
    }

    public void processMilkTea(MilkTea milkTea) {
        String name = milkTea.getName();
        System.out.println(name);
        milkTea.addHot();
        milkTea.addIce();
        milkTea.addSugar();
    }
}


/**
 * 工厂方法抽象类
 */
abstract class AbstractFactory {
    /**
     * 工厂方法抽象方法
     */
    public abstract MilkTea productMilkTea();
}


class StrawberryFactory extends AbstractFactory {
    @Override
    public MilkTea productMilkTea() {
        return new StrawberryMilkTea();
    }
}

class WaterMelonFactory extends AbstractFactory {
    @Override
    public MilkTea productMilkTea() {
        return new WaterMelonMilkTea();
    }
}

/**
 * 奶茶类-基类
 */
abstract class MilkTea {

    public abstract String getName();

    public void addIce() {
        System.out.println("加冰...");
    }

    public void addSugar() {
        System.out.println("加糖...");
    }

    public void addHot() {
        System.out.println("加热...");
    }
}

/**
 * 草莓为奶茶
 */
class StrawberryMilkTea extends MilkTea {
    @Override
    public String getName() {
        return "草莓奶茶";
    }
}

/**
 * 西瓜味奶茶
 */
class WaterMelonMilkTea extends MilkTea {
    @Override
    public String getName() {
        return "西瓜奶茶";
    }
}